package com.googlecode.maratische.google.gui;

import static com.googlecode.maratische.google.MainController.getMainController;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JWindow;
import javax.swing.Timer;

import org.apache.log4j.Logger;

import com.googlecode.maratische.google.model.Feed;

public class InfWindowAddOfflineMode extends JWindow implements ActionListener{
	static Logger logger = Logger.getLogger(InfWindowAddOfflineMode.class.getName());

	private static final long serialVersionUID = -3493460193936165136L;
	private Feed feed = null;
	protected JButton buttonCancel = null;
	protected Timer timer = new Timer(1000, this);
	protected int timerPosition = 10;

	protected InfWindowAddOfflineMode(Point parentPosition, Dimension parentSize) {
		double width = parentSize.getWidth()/2;
		width = (width < 500) ? 500 : width; 
		double height = parentSize.getHeight();
		height = (height < 100) ? 100 : height; 
		height = (height > 200) ? 200 : height; 
//		logger.info("width "+width+" height "+height);
		parentPosition.move((int) (parentPosition.x+parentSize.getWidth()-width), parentPosition.y);
		parentSize.setSize((int)width, height);
		setLocation(parentPosition);
		setSize(parentSize);
		getContentPane().setBackground(Color.yellow);
		setVisible(true);
	}
	
	public InfWindowAddOfflineMode(Feed feed, Point parentPosition, Dimension parentSize) {
		this(parentPosition, parentSize);
		this.feed = feed;
		init();
		initButtons();
	}

	public void init() {
		
		setLayout(new FlowLayout());
		StringBuilder name = new StringBuilder();
		name.append(getBundleMessage("infwindow.addofflinemode.question.1"));
		name.append(" \"").append(feed.getName()).append("\" ");
		name.append(getBundleMessage("infwindow.addofflinemode.question.2"));
		JLabel label = new JLabel(name.toString());
		label.setToolTipText(name.toString());
		add(label);
	}
	
	protected void initButtons() {
		JButton buttonYes = new JButton(getBundleMessage("button.yes"));
		buttonYes.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				buttonYesAction();
			}
		});
		JButton buttonNo = new JButton(getBundleMessage("button.no"));
		buttonNo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				buttonNoAction();
			}
		});
		buttonCancel = new JButton(getBundleMessage("button.cancel"));
		buttonCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getMainController().changeOfflineSupportForSelectedFeed(false);
				close();
			}
		});
		add(buttonYes);
		add(buttonNo);
		add(buttonCancel);
		
		timer.start();
	}

	protected void buttonYesAction() {
		getMainController().changeOfflineSupportForSelectedFeed(true);
		close();
	}

	protected void buttonNoAction() {
		getMainController().changeOfflineSupportForSelectedFeed(false);
		close();
	}
	
	protected void close() {
		if (timer.isRunning()) {
			timer.stop();
			timer.removeActionListener(this);
		}
		timer = null;
		dispose();
	}
	@Override
	public void actionPerformed(ActionEvent e) {
//		logger.info("tick! ");
		if (timerPosition < 1) {
			timer.stop();
//			logger.info("timer STOP! ");
			timer.removeActionListener(this);
			close();
			return;
		}
		buttonCancel.setText(getBundleMessage("button.cancel")+" ("+ timerPosition-- + ")");
	}
	
	public String getBundleMessage(String message) {
		return getMainController().getBundleMessage(message);
	}
	
}
